package com.example.smartcitymovie.utils;

import com.example.smartcitymovie.MyApplication;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @Author: Machu
 * @Time: 2022/2/14 10:21
 * @Description: 功能描述
 */
public class OkHttpUtils {
    public static String BaseUrl = "http://124.93.196.45:10001";
//    public static String BaseUrl = "http://10.10.2.77:10001";
    public static Call getCall(String url){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(BaseUrl+url)
                .method("GET",null)
                .addHeader("Authorization", MyApplication.getSharedPreferences().getString("Token",""))
                .build();
        return client.newCall(request);
    }

    public static Call postCall(String url,String json){

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(BaseUrl+url)
                .method("POST", body)
                .addHeader("Authorization", MyApplication.getSharedPreferences().getString("Token",""))
                .addHeader("Content-Type", "application/json")
                .build();
        return  client.newCall(request);
    }

    public static Call postCall(String url){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(BaseUrl+url)
                .method("POST", null)
                .addHeader("Authorization", MyApplication.getSharedPreferences().getString("Token",""))
                .addHeader("Content-Type", "application/json")
                .build();
        return  client.newCall(request);
    }

    public static Call putCall(String url,String json){

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(BaseUrl+url)
                .method("PUT", body)
                .addHeader("Authorization", MyApplication.getSharedPreferences().getString("Token",""))
                .addHeader("Content-Type", "application/json")
                .build();
        return  client.newCall(request);
    }
}
